package com.gxa.zhonglong.service.impl;

import com.gxa.zhonglong.common.ExceptionCode;
import com.gxa.zhonglong.exception.BusinessException;
import com.gxa.zhonglong.mapper.ClasslibMapper;
import com.gxa.zhonglong.mapper.HeadquartersMapper;
import com.gxa.zhonglong.mapper.StoreReturnMapper;
import com.gxa.zhonglong.pojo.StoreBarter;
import com.gxa.zhonglong.service.StoreReturnService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author wanjun
 * @version 1.0
 * @date 2021/10/30 12:55
 */
@Transactional(rollbackFor = Throwable.class)
@Service
public class StoreReturnServiceImpl implements StoreReturnService {
    @Autowired
    StoreReturnMapper storeReturnMapper=null;
    @Autowired
    ClasslibMapper classlibMapper=null;
    @Autowired
    HeadquartersMapper headquartersMapper=null;
    //获得总部下边类库名字
    @Override
    public List<String> getClassLibName(String headName) throws BusinessException {
        List<Map<String, Object>> list1 = headquartersMapper.selectAll();
        List<String> stringList=new ArrayList<>();
        for (Map<String, Object> map : list1) {
            if(((String)map.get("name")).equals(headName)){
                List<Map<String, Object>> list = classlibMapper.selectByHeadquartersId((Long) map.get("id"));
                System.out.println(list);
                for (Map<String, Object> map1 : list) {
                    String string=(String)map1.get("cname");
                    stringList.add(string);
                }
                System.out.println(stringList);
                return stringList;
            };
        }
       throw new BusinessException(ExceptionCode.SERVER);
    }

    //获得所有总部的名字
    @Override
    public List<String> getAllHead() {
        List<Map<String, Object>> list1 = headquartersMapper.selectAll();
        System.out.println(list1);
        List<String> stringList1=new ArrayList<>();
        for (Map<String, Object> map : list1) {
            String string1=(String)map.get("name");
            stringList1.add(string1);
        }
        return stringList1;
    }
    @Override
    public List<StoreBarter> selectAll(Integer offset, Integer size) {
        List<StoreBarter> storeBarters = storeReturnMapper.selectAll((offset - 1) * size, size);
        return storeBarters;
    }

    @Override
    public List<StoreBarter> selectAll2() {
        List<StoreBarter> storeBarters2 = storeReturnMapper.selectAll2();
        return storeBarters2;
    }

    @Override
    public List<StoreBarter> selectByField(Timestamp from, Timestamp to, String storeBarterGclass, String storeBarterNews, String storeBarterNewc, String storeBarterOlds, String storeBarterOldc, String storeBarterCusername, Integer offset, Integer size) {
        List<StoreBarter> storeBarters = storeReturnMapper.selectByField(from, to, storeBarterGclass, storeBarterNews, storeBarterNewc, storeBarterOlds, storeBarterOldc, storeBarterCusername, (offset-1)*size, size);
        return storeBarters;
    }

    @Override
    public List<StoreBarter> selectByField2(Timestamp from, Timestamp to, String storeBarterGclass, String storeBarterNews, String storeBarterNewc, String storeBarterOlds, String storeBarterOldc, String storeBarterCusername) {
        List<StoreBarter> storeBarters2 = storeReturnMapper.selectByField2(from, to, storeBarterGclass, storeBarterNews, storeBarterNewc, storeBarterOlds, storeBarterOldc, storeBarterCusername);
        return storeBarters2;
    }
    @Override
    public void add(StoreBarter storeBarter,String storeBarterNum, List<String> goodsCoding) {
        storeReturnMapper.add(storeBarter);
        storeReturnMapper.addCo(storeBarterNum,goodsCoding);
    }
}
